﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Domain.Query.MaterialModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Cache;

namespace Resto.CostControl.Core.Facade.MaterialModule.FacadeObject
{
    /// <summary>
    /// 品名FO
    /// </summary>
    public class MaterialFO : IApplicationFacadeObject
    {
        #region 私有属性

        private MaterialDQO _MaterialDQO;
        private MaterialDQO MaterialDQO
        {
            get
            {
                if (_MaterialDQO == null)
                    _MaterialDQO = ObjectCreator.Create<MaterialDQO>();

                return _MaterialDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddMaterial(MaterialDTO materialDTO)
        {
            Material material = materialDTO.ToMaterial();
            material.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            materialDTO.ID = material.ID;
        }

        public virtual void UpdateMaterial(MaterialDTO materialDTO)
        {
            Material material = MaterialDQO.GetMaterial(materialDTO.ID);
            materialDTO.CopyToMaterial(material);

            RestoCostDomainSession.CurrentSession.SaveChanges();

            MaterialDTOCache.Remove(material.ID);
        }

        public virtual void DeleteMaterial(string id)
        {
            Material material = MaterialDQO.GetMaterial(id);

            material.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            MaterialDTOCache.Remove(material.ID);
        }

        public virtual MaterialDTO GetMaterialDTO(string id)
        {
            Material material = MaterialDQO.GetMaterial(id);
            if (material.IsDelete)
                return null;

            return new MaterialDTO(material);
        }

        #endregion
    }
}
